﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

using System;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.BL.DM.Test.DataManagement;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Monitoring;
using ScrumTable.Common.Security;
using ScrumTable.Common.Threading;
using ScrumTable.Config.ElementManagement;
using ScrumTable.DL.Data;
using ScrumTable.DL.Data.Common;
using ScrumTable.DL.Data.Local;
using ScrumTable.DL.Data.Local.Test;
using ScrumTable.DL.Data.Schema.ScrumTableV1;


namespace ScrumTable.Config.Test.ElementManager
{
    /// <summary>
    /// This is the test class for LoginAccountManager class.
    /// </summary>
    [TestClass]
    public class ElementManagerTest
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private ScrumTable.Config.ElementManagement.ElementManager _context;
        private IDomainContext _domainContext;

        private const string ProjectName = "_TEST_PROJECT_";
        

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DomainContextFactoryTest instance.
        /// </summary>
        public ElementManagerTest()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Use TestInitialize to run code before running each test .
        /// </summary>
        [TestInitialize]
        public void TestInitialize()
        {
            _context = ElementManagerFactory.CreateConfigManager(new Guid().ToString(), new Guid().ToString());


            var dataContext = DataConnectionFactory.Create(DataAssembly.FromAssembly(typeof(DataContext).Assembly));
            _domainContext = DomainContextFactory.Create(dataContext);
            _domainContext.Connect(
                new SyncDataDispatcher(),
                new ConnectionInformationManager(ConnectionInformation.Empty),
                delegate { },
                Resources.GetProjectTestDataFilePath(TestContext.TestDir));

            _domainContext.Projects[Resources.Project2Id].LoadData(new NullDataImportMonitor());
            _domainContext.Projects[Resources.Project1Id].LoadData(new NullDataImportMonitor());

            
        }

        /// <summary>
        /// Use TestCleanup to run code after each test has run.
        /// </summary>
        [TestCleanup]
        public void TestCleanup()
        {
            _context = null;
        }


        [TestMethod]
        public void TestLoadDataFromProject()
        {
            _context.SetCurrentConfig(_domainContext.Projects[Resources.Project1Id]);

            Assert.AreEqual(_context.ElementConfig.StateConfigBug.Count, _domainContext.Projects[Resources.Project1Id].Types.BugType.StateMappings.Count);
            Assert.AreEqual(_context.ElementConfig.StateConfigStory.Count, _domainContext.Projects[Resources.Project1Id].Types.UserStoryType.StateMappings.Count);
            Assert.AreEqual(_context.ElementConfig.StateConfigTask.Count, _domainContext.Projects[Resources.Project1Id].Types.TaskType.StateMappings.Count);
        }



        [TestMethod]
        public void TestStateMapping()
        {
            _context.SetCurrentConfig(_domainContext.Projects[Resources.Project1Id]);

            Assert.AreEqual(_context.ElementConfig.StateConfigBug.First(x => x.Right == "Closed"), (_context.ElementConfig.StateConfigStory.First(x => x.Right == "Closed")));
            Assert.AreEqual(_context.ElementConfig.StateConfigBug.First(x => x.Right == "Closed"), (_context.ElementConfig.StateConfigTask.First(x => x.Right == "Closed")));
        }


        [TestMethod]
        public void TestTakeOverCustomValues()
        {
            _context.SetCurrentConfig(_domainContext.Projects[Resources.Project1Id]);
            int defaultValue = _context.ElementConfig.StateConfigBug[0].Left;
            int newValue = defaultValue + 10;

            _context.ElementConfig.StateConfigBug[0].Left = newValue;

            //change to an other project -> default settings for the other project
            _context.SetCurrentConfig(_domainContext.Projects[Resources.Project2Id]);
            Assert.AreEqual(_context.ElementConfig.StateConfigBug[0].Left, defaultValue);

            //change back and check if the value has been restored
            _context.SetCurrentConfig(_domainContext.Projects[Resources.Project1Id]);
            Assert.AreEqual(_context.ElementConfig.StateConfigBug[0].Left, newValue);
        }

        [TestMethod]
        public void TestGetDefaultTargetStoryBoard()
        {
            _context.SetCurrentConfig(_domainContext.Projects[Resources.Project1Id]);
            var target = _context.GetTargetConfig("DailyScrumStoryBoard");
            Assert.IsTrue(target.IsBug);
            Assert.IsTrue(target.IsStory);
            Assert.IsFalse(target.IsTask);

            var states = _context.GetStates(target);
            foreach (var state in _context.ElementConfig.StateConfigStory)
            {
                var state1 = state;
                Assert.IsNotNull(states.FirstOrDefault(x => x.Name == state1.Right));
            }

            foreach (var state in _context.ElementConfig.StateConfigBug)
            {
                var state1 = state;
                Assert.IsNotNull(states.FirstOrDefault(x => x.Name == state1.Right));
            }
            Assert.IsNull(states.FirstOrDefault(x => x.Name == "ThisIsNoState"));


        }




        #endregion


 
    }
}